package com.huaao.micropolice.root.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.huaao.micropolice.common.util.JsonUtil;
import com.huaao.micropolice.core.common.Context;
import com.huaao.micropolice.core.model.system.User;
import com.huaao.micropolice.core.service.system.UserService;
import com.huaao.micropolice.root.controllers.BaseController;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 
 * @author Zhangyu
 * {@docRoot 用户接口类}、
 * @version 1.0
 */
@Controller
@RequestMapping("UserInfo")
public class UserController extends BaseController {
	@Autowired
	UserService userService;
	private static JsonConfig jsonConfig = new JsonConfig();
	static{
		jsonConfig.setExcludes(new String[]{"roles","dept","jobs","handler","hibernateLazyInitializer"});
	}

//	/**
//	 * 查询所有用户
//	 * 
//	 * @return
//	 */
//	@RequestMapping(value = "/findAllUser.do", method = RequestMethod.POST)
//	public ResponseEntity<String>  findAllUserInfo(){
//			
//		return null;
//	}
//	
//	
//	/**
//	 * 模糊查询查询所有用户
//	 * 
//	 * @return
//	 */
//	@RequestMapping(value = "/findAllUserToLike.do", method = RequestMethod.POST)
//	public ResponseEntity<String>  findAllUserInfoLike(){
//		
//		return null;
//	}
//	
	/**
	 * 根据当前登录的用户查询当前的用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "/findUserInfo.do", method = RequestMethod.POST)
	public ResponseEntity<String>  findUserInfo(){
		User user = Context.getCurrentUser();
		User result=userService.findByUserid(user.getId());
		if(result!=null){
		result.setPassword("");
		return super.ajaxModel(JSON,JSONObject.fromObject(result,jsonConfig).toString());
		}else{
		return super.ajaxModel(JSON,null);
		}
	}
	
	/**
	 * 修改密码
	 * 1.先判断当前密码是否是有效正确的
	 * 2.查询当前用户是否存在 
	 * 3.存在则修改密码
	 * 4.不存在则返回错误消息
	 * 5.无效则提示错误
	 * 6.有效则修改密码
	 * 7.更新session中保存的用户信息
	 * @param password 旧密码
	 * @param newPassword 新密码
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/editPassword.do", method = RequestMethod.POST)
	public ResponseEntity<String> editPassword(String password,String newpassword) throws Exception{

		User user = Context.getCurrentUser();

		User passwordUser=userService.findByUserid(user.getId());
		if(passwordUser==null){
			
			return  super.ajaxModel(JSON,"用户不正确");
		}
		if(!passwordUser.getPassword().equals(password)){
			return	super.ajaxModel(JSON,"密码不正确");
		}
		user.setPassword(newpassword);
		userService.updateUser(passwordUser);
		Context.setCurrentUser(passwordUser);
		return super.ajaxModel(JSON,MESSAGE_SUCCESS);
	}
	
	
	
	/**
	 * 修改用户资料
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/updateUserInfo.do", method = RequestMethod.POST)
	public ResponseEntity<String>  updateUserInfo(String email,String phone,String username,String sex,String realname) throws Exception{
		User user = Context.getCurrentUser();
		User passwordUser=userService.findByUserid(user.getId());
		passwordUser.setEmail(email);
		passwordUser.setPhone(phone);
		passwordUser.setUsername(username);
		passwordUser.setSex(sex);
		passwordUser.setRealname(realname);
		userService.updateUser(passwordUser);
		Context.setCurrentUser(passwordUser);
		return super.ajaxModel(HTML,MESSAGE_SUCCESS);
	}
	
	
}
